package cn.gx.kevin.common.ui;

import cn.gx.kevin.common.utils.SysConfigParams;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @ClassName: LanguageStore
 * @Description: TODO
 * @author hjwen
 * @date 2018年10月10日 下午8:45:01
 */
public class LanguageStore {
	final static Logger log = LoggerFactory.getLogger(LanguageStore.class);
	private static Map<String, LanguageMap> languageMap = new HashMap<String, LanguageMap>();



	/**
	 * @Title: getLanguageStore
	 * @Description: TODO
	 * @return
	 * @throws
	 */
	public static Map<String, LanguageMap> getLanguageStore(){
		return languageMap;
	}


	public static Map<String,String> getLangHashMap(String key){
		if(StringUtils.isEmpty(key)){
			key  = SysConfigParams.getConfig("lang");
		}
		LanguageMap languageMap = LanguageStore.getLanguageMap(key);
		Map<String, String> langMap = languageMap.getLangMap();
		return langMap;
	}

	/**
	 * @Title: getLanguageMap
	 * @Description: 根据语言key获取 语言map
	 * @param key
	 * @return
	 * @throws
	 */
	public static LanguageMap getLanguageMap(String key){
		if(StringUtils.isEmpty(key)){
			key  = SysConfigParams.getConfig("lang");
		}
		LanguageMap map  = languageMap.get(key);
		if(map == null){
			log.error("lang {} is null ",key);
			log.error(JSONObject.toJSONString(languageMap));
		}
		return map;
	}

	/**
	 * @Title: getValueByKey
	 * @Description: 根据key获取配置的值
	 * @param lang
	 * @param key
	 * @return
	 * @throws
	 */
	public static String getValueByKey(String lang,String key){
		LanguageMap currentLanguageMap = languageMap.get(lang);
		return currentLanguageMap.getLangByKey(key);
	}

	/***
	 * 加载语言配置
	 * ***/
	public static void loadLangConfig(String resourcePath) throws Exception{
		if(StringUtils.isEmpty(resourcePath)){
			resourcePath = "classpath*:/lang/**/*.properties";
		}
		log.info("start to load lang config ♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣");
		Map<String, LanguageMap> languageMap = LanguageStore.getLanguageStore();
		PathMatchingResourcePatternResolver resoler = new PathMatchingResourcePatternResolver();
		Resource[] resources = resoler.getResources(resourcePath);
		Pattern pattern = Pattern.compile("/lang/(\\w+)/");
		for(Resource resource : resources){
			String path = resource.getURI().toString();
			if(log.isDebugEnabled()){
				log.info("language file " + path);
			}
			Matcher matche = pattern.matcher(path);
			String langName = "";
			while (matche.find()){
				langName = matche.group(1);
				if(log.isDebugEnabled()){
					log.info("is find path lang name {}",langName);
				}
				break;
			}
			LanguageMap langMap = languageMap.get(langName);
			if(langMap == null){
				langMap = new LanguageMap();
				languageMap.put(langName,langMap);
			}
			InputStream stream =  resource.getInputStream();
			Properties properties = new Properties();
			properties.load(stream);
			stream.close();
			for (Object key : properties.keySet()) {
				String k = String.valueOf(key);
				String v = properties.getProperty(k);
				langMap.setLangKeyValue(k, v);
			}
		}
	}
}
